package com.smalls.redshoes.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileUtils {
    private String SDPATH = CacheUtils.getCachePath(CacheUtils.DIRECTORY_PICTURES);

    public String getSDPATH() {
        return this.SDPATH;
    }

    public File createSDFile(String fileName) throws IOException {
        File file = new File(this.SDPATH + fileName);
        file.createNewFile();
        return file;
    }

    public File createSDDir(String dirName) {
        File dir = new File(this.SDPATH + dirName);
        dir.mkdir();
        return dir;
    }

    public boolean isFileExist(String fileName) {
        return new File(this.SDPATH + fileName).exists();
    }

    public void deleteFile(String fileName) {
        new File(this.SDPATH + fileName).delete();
    }

    public File write2SDFromInput(String path, InputStream inputstream) {
        IOException e;
        Throwable th;
        File file = null;
        OutputStream outputStream = null;
        try {
            file = createSDFile(path);
            OutputStream output = new FileOutputStream(file);
            try {
                byte[] buffer = new byte[4096];
                while (true) {
                    int temp = inputstream.read(buffer);
                    if (temp == -1) {
                        break;
                    }
                    output.write(buffer, 0, temp);
                }
                output.flush();
                if (output != null) {
                    try {
                        output.close();
                    } catch (IOException e2) {
                        e2.printStackTrace();
                        outputStream = output;
                    }
                }
                outputStream = output;
            } catch (IOException e2) {
                outputStream = output;
                try {
                    e2.printStackTrace();
                    if (outputStream != null) {
                        try {
                            outputStream.close();
                        } catch (IOException e22) {
                            e22.printStackTrace();
                        }
                    }
                    return file;
                } catch (Throwable th2) {
                    th = th2;
                    if (outputStream != null) {
                        try {
                            outputStream.close();
                        } catch (IOException e222) {
                            e222.printStackTrace();
                        }
                    }
                }
            } catch (Throwable th3) {
                th = th3;
                outputStream = output;
                if (outputStream != null) {
                    outputStream.close();
                }
            }
        } catch (IOException e4) {
            e4.printStackTrace();
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            return file;
        }
        return file;
    }

    public void deleteDir(File file) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File deleteDir : files) {
                deleteDir(deleteDir);
            }
        }
        file.delete();
    }
}
